import { request } from '@/utils/axios/axios'
import type { Role, AddOrUpdateRoleResponse, RoleResponse, RoleMenuVo } from '@/api/role/type'

// 角色API
enum API {
  ROLE = 'role',
  ADD_MENU_TO_ROLE = 'role/addmenutorole',
}

// 查询全部角色信息
export const queryRoleList = (): Promise<Role[]> => {
  return request.get<null, Role[]>(API.ROLE)
}
// 添加角色
export const addRoleRequest = (roleName: string): Promise<AddOrUpdateRoleResponse> => {
  return request.post<string, AddOrUpdateRoleResponse>(API.ROLE + `/${roleName}`)
}
// 查询全部角色信息
export const queryAllRolesRequest = (
  pageIndex: number,
  pageSize: number,
  condition: string,
): Promise<RoleResponse> => {
  return request.get<string, RoleResponse>(API.ROLE + `/${pageIndex}/${pageSize}/${condition}`)
}
// 更新角色名称
export const updateRoleRequest = (role: Role): Promise<AddOrUpdateRoleResponse> => {
  return request.post<Role, AddOrUpdateRoleResponse>(API.ROLE, role)
}
// 为角色分配权限请求
export const assignMenuToRoleRequest = (roleMenuVo: RoleMenuVo): Promise<string> => {
  return request.post<RoleMenuVo, string>(API.ADD_MENU_TO_ROLE, roleMenuVo)
}
// 查询角色已有权限的请求
export const queryRolePossessedMenusRequest = (roleId: number): Promise<number[]> => {
  return request.get<number, number[]>(API.ROLE + `/${roleId}`)
}
// 删除角色
export const deleteRoleRequest = (role: Role) => {
  return request.delete(API.ROLE, { data: role })
}
